package controlleruploadfile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UpfileServlet extends  HttpServlet{
	
	
	/**
	 * http://localhost:8090/java-fileupload/fileUpload.jsp
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
//		System.out.println("in servlet");
		
		request.setCharacterEncoding("utf-8");
		
		try {
			// Create a factory for disk-based file items
			DiskFileItemFactory factory = new DiskFileItemFactory();

			// Configure a repository (to ensure a secure temp location is used)
			ServletContext servletContext = this.getServletConfig().getServletContext();
			
			File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
			factory.setRepository(repository);

			// Create a new file upload handler
			ServletFileUpload upload = new ServletFileUpload(factory);

			// Parse the request
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> iter = items.iterator();
			while (iter.hasNext()) {
			    FileItem item = iter.next();

			    String name = item.getName();
			    String st = item.getString();
			    System.out.println(name);
			    System.out.println(st);
			    System.out.println(item.isFormField());
			    if (item.isFormField()) {
			    	//文件说明
			    	 name = item.getName();
				     st = item.getString();
				    System.out.println(name);
				    System.out.println(st);
			        
			    } else {
			    	
			    	//要上传的文件名
			    	String fileName = item.getName();
			    	
			    	
			    	//服务器路径
			    	String root = request.getSession().getServletContext().getRealPath("/liuhaofileupload");
			    	
			    	System.out.println(root);
			    	
			    	File filepath=new File(root+File.separator+fileName);
				    
				    InputStream is = item.getInputStream();
				    
				    FileOutputStream fos=new FileOutputStream(filepath);
				    
				    byte[] b=new byte[1024];
				    int len=0;
				    while((len =is.read(b))!=-1){
				    	fos.write(b, 0, len);
				    	fos.flush();
				    }
				    
				    //存放在session中
				    request.getSession().setAttribute("img","liuhaofileupload"+File.separator+fileName);
				    
				    response.sendRedirect("jg.jsp");
				    fos.close();
				    is.close();
				    
				    		
				    
				    
				    
				    
				    
				    
				    
				   /* byte[] bytes = st.getBytes();
				    int length = st.length();
				    
				    FileOutputStream fos=new FileOutputStream("c:/aa.jpg");
				    fos.write(bytes, 0, length);
				    fos.flush();
				    fos.close();*/
				    
				    
				    
				  /*  File fullFile = new File(item.getName());
				    
				    //上传文件目录
				    File uploadPath = new File("c:/a.jpp");
				    File savedFile = new File(uploadPath, fullFile.getName());
				    try {
						item.write(savedFile);
					} catch (Exception e) {
						e.printStackTrace();
					}*/
				    
			        
			    }
			}
		
			
		} catch (FileUploadException e) {
			
			e.printStackTrace();
		}
		
	}

}
